package com.example.xpvideo.tools;

import com.alibaba.fastjson.JSON;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.Map;

/**
 * @author Archer
 */
@Component
public class HttpClientTools {
    public String httpGet(String url) {
        String result = "";
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            //创建HttpClient对象
            httpClient = HttpClients.createDefault();
            //创建HttpGet对象
            HttpGet httpGet = new HttpGet(url);
            // HttpHost proxy = new HttpHost("183.166.174.93", 49117, "http");
            RequestConfig requestConfig = RequestConfig.custom()
                    //设置连接超时时间
                    .setConnectTimeout(300000)
                    //从连接池获取连接超时时间
                    .setConnectionRequestTimeout(300000)
                    //设置读取数据超时时间
                    .setSocketTimeout(300000)
                    // .setProxy(proxy)
                    .build();
            httpGet.setConfig(requestConfig);
            response = httpClient.execute(httpGet);
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            close(httpClient, response);
        }
        return result;
    }


    public String httpPost(String url,
                           Map<String, Object> params,
                           Map<String, String> headers) {
        String result = "";
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            //创建HttpClient对象
            httpClient = HttpClients.createDefault();
            //创建HttpPost对象
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/json");
            String json = JSON.toJSONString(params);
            StringEntity se = new StringEntity(json, "UTF-8");
            se.setContentType("text/json");
            httpPost.setEntity(se);
            if (!CollectionUtils.isEmpty(headers)) {
                headers.forEach(httpPost::setHeader);
            }
            RequestConfig requestConfig = RequestConfig.custom()
                    //设置连接超时时间
                    .setConnectTimeout(30000)
                    //从连接池获取连接超时时间
                    .setConnectionRequestTimeout(30000)
                    //设置读取数据超时时间
                    .setSocketTimeout(30000)
                    .build();
            httpPost.setConfig(requestConfig);
            response = httpClient.execute(httpPost);
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            close(httpClient, response);
        }
        return result;
    }

    public String httpPostForm(String url,
                               Map<String, Object> params,
                               Map<String, String> headers) {

        String result = "";
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            //创建HttpClient对象
            httpClient = HttpClients.createDefault();
            //创建HttpPost对象
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/json");
            String json = JSON.toJSONString(params);
            StringEntity se = new StringEntity(json, "UTF-8");
            se.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
            httpPost.setEntity(se);
            if (!CollectionUtils.isEmpty(headers)) {
                headers.forEach(httpPost::setHeader);
            }
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(30000)//设置连接超时时间
                    .setConnectionRequestTimeout(30000)//从连接池获取连接超时时间
                    .setSocketTimeout(30000)//设置读取数据超时时间
                    .build();
            httpPost.setConfig(requestConfig);
            response = httpClient.execute(httpPost);
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            close(httpClient, response);
        }
        return result;
    }

    private void close(CloseableHttpClient httpClient,
                       CloseableHttpResponse response) {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (httpClient != null) {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
